﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Util.NoSQLHelpers;

namespace AllSqlHelperWin.NoSQLHelperForms.MongoDBForms
{
    public partial class MongoDBAddForm : Form
    {
        public MongoDBAddForm()
        {
            InitializeComponent();
        }

        private void MysqlAddForm_Load(object sender, EventArgs e)
        {
            cmbClass.SelectedIndex = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string name = txtName.Text.Trim();
                if (string.IsNullOrEmpty(name))
                {
                    MessageBox.Show("名称不可为空！");
                    return;
                }

                string classStr = cmbClass.Text.Trim();
                string ageStr = txtAge.Text.Trim();
                bool gender = rdBtnB.Checked;

                UserModel_MongoDB model = new UserModel_MongoDB()
                {
                    Name = name,
                    Class = classStr,
                    Age = string.IsNullOrEmpty(ageStr) ? 0 : Convert.ToInt32(ageStr),
                    Gender = gender,
                    CreateTime = DateTime.Now
                };

                var collection = MongoDBHelper.GetCollection<UserModel_MongoDB>("User");
                MongoDBHelper.InsertOne<UserModel_MongoDB>(collection, model);
                MessageBox.Show("添加成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败！错误信息：" + ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string name = txtName.Text.Trim();
                if (string.IsNullOrEmpty(name))
                {
                    MessageBox.Show("名称不可为空！");
                    return;
                }

                string classStr = cmbClass.Text.Trim();
                string ageStr = txtAge.Text.Trim();
                bool gender = rdBtnB.Checked;

                List<UserModel_MongoDB> models = new List<UserModel_MongoDB>();

                for (int i = 0; i < 3; i++)
                {
                    models.Add(new UserModel_MongoDB()
                    {
                        Name = name,
                        Class = classStr,
                        Age = string.IsNullOrEmpty(ageStr) ? 0 : Convert.ToInt32(ageStr) + i,
                        Gender = gender,
                        CreateTime = DateTime.Now
                    });
                }

                var collection = MongoDBHelper.GetCollection<UserModel_MongoDB>("User");
                MongoDBHelper.InsertMany<UserModel_MongoDB>(collection, models);

                MessageBox.Show($"添加成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败！错误信息：" + ex.Message);
            }
        }

        /// <summary>
        /// 只能输入数字
        /// </summary>
        private void txt1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果输入的不是退格和数字，则屏蔽输入
            if (!(e.KeyChar == 8 || (e.KeyChar >= 48 && e.KeyChar <= 57)))  // 小数点为46
            {
                e.Handled = true;
            }
        }
    }
}
